﻿using System;
using System.Xml.Serialization;
using System.ComponentModel;
using Kiva7.Translations;
using Kiva7.ViewModels;

namespace Kiva7.DataClasses
{
    // loan
    public class Loan : BaseViewModel
    {
        [XmlElement("terms")]
        public Terms Terms { get; set; }

        [XmlElement("id")]
        public int Id { get; set; }
        [XmlElement("name")]
        public string Name { get; set; }
        [XmlElement("status")]
        public LoanStatus? Status { get; set; }

        [XmlElement("loan_amount")]
        public double LoanAmount { get; set; }

        [XmlIgnore]
        public double LoanAmountActual
        {
            get
            {
                if (Terms != null)
                {
                    return Terms.LoanAmount;
                }
                else
                {
                    return LoanAmount;
                }
            }
        }

        [XmlIgnore]
        public string Link
        {
            get
            {
                return "http://www.kiva.org/lend/" + Id;
            }
        }

        [XmlElement("funded_amount")]
        public double FundedAmount { get; set; }

        [XmlIgnore]
        public double FundsNeeded
        {
            get
            {
                return LoanAmountActual - FundedAmount;
            }
        }

        [XmlIgnore]
        public double FundedPercentage
        {
            get
            {
                double loanAmount = LoanAmountActual;
                if (loanAmount == 0)
                {
                    return 0;
                }
                else
                {
                    return (FundedAmount / loanAmount) * 100;
                }
            }
        }

        [XmlElement("paid_amount")]
        public double PaidAmount { get; set; }

        [XmlIgnore]
        public double PaidPercentage
        {
            get
            {
                double loanAmount = LoanAmountActual;
                if (loanAmount == 0)
                {
                    return 0;
                }
                else
                {
                    return (PaidAmount / loanAmount) * 100;
                }
            }
        }


        [XmlElement("borrower_count")]
        public int BorrowerCount { get; set; }
        [XmlElement("image")]
        public Image Image { get; set; }
        [XmlElement("activity")]
        public string Activity { get; set; }
        [XmlElement("sector")]
        public string Sector { get; set; }
        [XmlElement("use")]
        public string Use { get; set; }

        private Translation _useTranslation;
        [XmlIgnore]
        public Translation UseTranslation
        {
            get
            {
                if (_useTranslation == null)
                {
                    _useTranslation = new Translation(Use);
                }
                return _useTranslation;
            }
        }

        [XmlElement("location")]
        public Location Location { get; set; }
        [XmlElement("partner_id")]
        public int PartnerId { get; set; }

        [XmlElement("posted_date")]
        public DateTime? PostedDate { get; set; }
        [XmlElement("funded_date")]
        public DateTime? FundedDate { get; set; }
        [XmlElement("paid_date")]
        public DateTime? PaidDate { get; set; }

        [XmlElement("description")]
        public Description Description { get; set; }

        [XmlElement("borrowers")]
        public Borrowers Borrowers { get; set; }
        [XmlElement("payments")]
        public Payments Payments { get; set; }

        [XmlElement("journal_totals")]
        public JournalTotals JournalTotals { get; set; }

        private bool _isSelected;
        [XmlIgnore]
        public bool IsSelected 
        {
            get
            {
                return _isSelected;
            }
            set
            {
                if (_isSelected != value)
                {
                    _isSelected = value;
                    NotifyPropertyChanged("IsSelected");
                }
            }
        }

        [XmlIgnoreAttribute]
        public CombinedPaymentsList CombinedPayments
        {
            get
            {
                CombinedPaymentsDictionary dic = new CombinedPaymentsDictionary();
                if (Payments != null
                    && Payments.PaymentItems != null)
                {
                    dic.AddPayments(Payments.PaymentItems);
                }
                if (Terms != null
                    && Terms.ScheduledPayments != null
                    && Terms.ScheduledPayments.ScheduledPaymentItems != null)
                {
                    dic.AddScheduledPayments(Terms.ScheduledPayments.ScheduledPaymentItems);
                }
                CombinedPaymentsList list = new CombinedPaymentsList();

                list.AddRange(dic.Values);

                return list;
            }
        }

    }
}

